package com.wf.ew.system.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.system.biz.BugBiz;
import com.wf.ew.system.model.Bug;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-12-26 10:52:51
 * @version ：1.0
 */
@Repository
public class BugBizImpl extends BaseDao implements BugBiz {

	@Override
	public String list(PageResult<T> page, Bug bug, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Bug> rm = new BeanPropertyRowMapper<Bug>(Bug.class);
		StringBuffer sql = new StringBuffer("select b.id,b.problems,b.type");
        sql.append(",b.state,b.person,b.checkedperson,b.remark");
		sql.append(",b.priority,b.createTime,b.createUser,b.updateTime");
		sql.append(",b.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_bug b ");
        sql.append(" inner join sys_user suser on suser.id=b.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=b.companyId ");
        sql.append(" where b.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and b.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(bug)){
//			if(Utils.isNotEmpty(bug.getName())){
//				sql.append(" and instr(b.name,:name)>0 ");
//				map.put("name", bug.getName());
//			}
			if(Utils.isNotEmpty(bug.getSort())&&Utils.isNotEmpty(bug.getOrder())){
				sql.append(" order by "+bug.getSort()+" "+bug.getOrder());
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Bug> list(Bug bug, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Bug> rm = new BeanPropertyRowMapper<Bug>(Bug.class);
        StringBuffer sql = new StringBuffer("select b.id,b.problems,b.type");
        sql.append(",b.state,b.person,b.checkedperson,b.remark");
		sql.append(",b.priority,b.createTime,b.createUser,b.updateTime");
		sql.append(",b.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_bug b ");
        sql.append(" inner join sys_user suser on suser.id=b.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=b.companyId ");
        sql.append(" where b.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and b.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(bug)){
//			if(Utils.isNotEmpty(bug.getName())){
//				sql.append(" and instr(b.name,:name)>0 ");
//				map.put("name", bug.getName());
//			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Bug getById(Bug bug) {
		RowMapper<Bug> rm = new BeanPropertyRowMapper<Bug>(Bug.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select b.id,b.problems,b.type");
        sql.append(",b.state,b.person,b.checkedperson,b.remark");
		sql.append(",b.priority,b.createTime,b.createUser,b.updateTime");
		sql.append(",b.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_bug b ");
        sql.append(" inner join sys_user suser on suser.id=b.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=b.companyId ");
        sql.append(" where b.del=0 and cmy.del=0 and b.id=:id ");
		map.put("id", bug.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(Bug bug, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_bug (");
        sql.append("problems,type,state,person");
		sql.append(",checkedperson,remark,priority,createUser) values ( ");
        sql.append(":problems,:type,:state,:person");
		sql.append(",:checkedperson,:remark,:priority,:createUser)");
        map.put("problems", bug.getProblems());
        map.put("type", bug.getType());
        map.put("state", bug.getState());
        map.put("person", bug.getPerson());
        map.put("checkedperson", bug.getCheckedperson());
        map.put("remark", bug.getRemark());
        map.put("priority", bug.getPriority());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Bug bug, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_bug set ");
        sql.append("problems=:problems,type=:type,state=:state ");
		sql.append(",person=:person,checkedperson=:checkedperson,remark=:remark ");
		sql.append(",priority=:priority,updateTime=now(),updateUser=:updateUser where id=:id ");
        map.put("problems", bug.getProblems());
        map.put("type", bug.getType());
        map.put("state", bug.getState());
        map.put("person", bug.getPerson());
        map.put("checkedperson", bug.getCheckedperson());
        map.put("remark", bug.getRemark());
        map.put("priority", bug.getPriority());
		map.put("updateUser", loginUser.getId());
		map.put("id", bug.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Bug bug, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(bug.getIds().length()>0){
			for(String ID : bug.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_bug set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
